package br.com.sadim.web;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

import br.com.sadim.paciente.Paciente;
import br.com.sadim.paciente.SexoPaciente;

public class RelatorioPacientePorSexoBean implements Serializable{
	
	private PacienteBean pacienteBean = new PacienteBean();
	private PieChartModel pieModel;
	private CartesianChartModel categoryModel;
	
	
	public RelatorioPacientePorSexoBean(){
		createPieModel();
		createCategoryModel();
	}
	
	public PieChartModel getPieModel() {
		if(pieModel == null)
			createPieModel();
        return pieModel;  
    }  
	
	private void createPieModel() {  
        pieModel = new PieChartModel();  
  
        getTotalHomens();
        getTotalMulheres();
    }  
	
    private void createCategoryModel() {  
    	categoryModel = new CartesianChartModel();  
  
    	ChartSeries boys = new ChartSeries();  
        boys.setLabel("Idade");  
        
        boys.set("0 a 18", getByIdade(0, 18));  
        boys.set("19 a 23", getByIdade(19, 23));  
        boys.set("24 a 28", getByIdade(24, 28));  
        boys.set("29 a 33", getByIdade(29, 33));  
        boys.set("34 a 38", getByIdade(34, 38));
        boys.set("39 a 43", getByIdade(39, 43));
        boys.set("44 a 48", getByIdade(44, 48));
        boys.set("49 a 53", getByIdade(49, 53));
        boys.set("54 a 58", getByIdade(54, 58));
        boys.set("59 +", getByIdade(59, 1000));
        
        categoryModel.addSeries(boys);  
    }  
	
    public static int getAge(Date dateOfBirth) {

        Calendar today = Calendar.getInstance();
        Calendar birthDate = Calendar.getInstance();

        int age = 0;

        birthDate.setTime(dateOfBirth);
        if (birthDate.after(today)) {
            throw new IllegalArgumentException("Can't be born in the future");
        }

        age = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);

        // If birth date is greater than todays date (after 2 days adjustment of leap year) then decrement age one year   
        if ( (birthDate.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR) > 3) ||
                (birthDate.get(Calendar.MONTH) > today.get(Calendar.MONTH ))){
            age--;

         // If birth date and todays date are of same month and birth day of month is greater than todays day of month then decrement age
        }else if ((birthDate.get(Calendar.MONTH) == today.get(Calendar.MONTH )) &&
                  (birthDate.get(Calendar.DAY_OF_MONTH) > today.get(Calendar.DAY_OF_MONTH ))){
            age--;
        }

        return age;
    }
    
    private int getByIdade(int min, int max) {
    	List<Paciente> pacientes = new ArrayList<Paciente>();
    	for(Paciente p : pacienteBean.getLista()){    				
    		int age = getAge(p.getDataNascimento());
			if(age > min && age <= max)
				pacientes.add(p);
		}
    	return pacientes.size();
    }
    
	public int getTotalHomens() {
		List<Paciente> pacientesHomens = new ArrayList<Paciente>();
		for(Paciente p : pacienteBean.getLista()){
			if(p.getSexo().equals(SexoPaciente.MASCULINO))
				pacientesHomens.add(p);
		}
		pieModel.set("Homes", pacientesHomens.size());  
		return pacientesHomens.size();
	}
	
	
	public int getTotalMulheres(){
		List<Paciente> pacientesMulheres = new ArrayList<Paciente>();
		for(Paciente p : pacienteBean.getLista()){
			if(p.getSexo().equals(SexoPaciente.FEMININO))
				pacientesMulheres.add(p);
		}
		pieModel.set("Mulheres", pacientesMulheres.size());  
		return pacientesMulheres.size();
	}
	
	public double getRateHomens(){
		double rate = 0.00;
		rate = (getTotalHomens()*1.0/(getTotalHomens()+getTotalMulheres())*1.0);
		return rate;
	}
	
	public double getRateMulheres(){
		double rate = 0.00;
		rate = (getTotalMulheres()*1.0/(getTotalHomens()+getTotalMulheres())*1.0);
		return rate;
	}

	public CartesianChartModel getCategoryModel() {
		return categoryModel;
	}

	public void setCategoryModel(CartesianChartModel categoryModel) {
		this.categoryModel = categoryModel;
	}

}
